###############################################                                     
# Cause of Effect? Turnout in Hispanic Majority-Minority Districts
#  - John A. Henderson, Jasjeet S. Sekhon, and Rocio Titiunik
#  - Forthcoming in Political Analysis
#  - Replication file for <Table VIII>
#  - April 14, 2016
###############################################

###########################################################################
#
## GENERATES TABLE VIII: Sensitivity Results for Observed Biases 2004 Results
#                                              
#
###########################################################################

options(width=150)
rm(list=ls())   

path='/local/'  
source(paste(path,'replicationPA/funs/headers.R',sep=''))

source(paste(path,'replicationPA/funs/amplifyUnobserved.R',sep=''))  
source(paste(path,'replicationPA/funs/amplifyObserved.R',sep=''))     
set.seed(57459)

imputes=FALSE
ties_used=FALSE    # do the full sample given already matching

#WH2data <- read.csv(file = paste(path,"Baseline-afterMATCH.csv",sep=''), header = TRUE, stringsAsFactors = FALSE)  
#WH2data <- read.dta(file = paste(path,"replicationPA/data/Baseline-afterMATCH-recreated.dta",sep=''))          
load(file = paste(path,"replicationPA/data/Baseline-afterMATCH-recreated.Rdata",sep=''))          
dim(WH2data)  

WH2data=WH2data[which(WH2data$dfound_pair_newdata==1),]  
dim(WH2data)
      
#Sdata <-  read.dta(file = paste(path,"replicationPA/data/Matched-recreated.dta",sep=''))
load(file = paste(path,"replicationPA/data/Matched-recreated.Rdata",sep=''))   
dim(Sdata)
      
Sdata=Sdata[which(Sdata$dfound_pair_newdata==1),]
dim(Sdata)


WH2data$hvap=WH2data$pop_hispanic18 
WH2data$nhvap=WH2data$vap-WH2data$hvap

WH2data$phh_income99_39less  = WH2data$phh_income99_0to19    + WH2data$phh_income99_20to39
WH2data$phh_income99_40to74  = WH2data$phh_income99_40to59   + WH2data$phh_income99_60to74
WH2data$phh_income99_100plus = WH2data$phh_income99_100to199 + WH2data$phh_income99_200
WH2data$ppop_foreign         = WH2data$ppop_foreign_naturcit + WH2data$ppop_foreign_nocit

WH2data$y98_reg_nhisp		 = WH2data$y98_reg_tot - WH2data$y98_reg_hisp 
WH2data$y98_preg_nhisp		 = WH2data$y98_reg_nhisp/WH2data$y98_reg_tot
       
WH2data$y00_turn_nhisp       = WH2data$y00_turn_tot - WH2data$y00_turn_hisp
WH2data$y00_reg_nhisp        = WH2data$y00_reg_tot - WH2data$y00_reg_hisp

WH2data$y04_turn_nhisp       = WH2data$y04_turn_tot - WH2data$y04_turn_hisp
WH2data$y04_reg_nhisp        = WH2data$y04_reg_tot  - WH2data$y04_reg_hisp                   

WH2data$y04_turn_nhisp       = WH2data$y04_turn_tot - WH2data$y04_turn_hisp
WH2data$y04_reg_nhisp        = WH2data$y04_reg_tot  - WH2data$y04_reg_hisp                                                                           
                                                                          
WH2data$y06_turn_nhisp       = WH2data$y06_turn_tot - WH2data$y06_turn_hisp
WH2data$y06_reg_nhisp        = WH2data$y06_reg_tot  - WH2data$y06_reg_hisp

Sdata$hvap=Sdata$pop_hispanic18 
Sdata$nhvap=Sdata$vap-Sdata$hvap  

Sdata$phh_income99_39less  = Sdata$phh_income99_0to19    + Sdata$phh_income99_20to39
Sdata$phh_income99_40to74  = Sdata$phh_income99_40to59   + Sdata$phh_income99_60to74
Sdata$phh_income99_100plus = Sdata$phh_income99_100to199 + Sdata$phh_income99_200
Sdata$ppop_foreign         = Sdata$ppop_foreign_naturcit + Sdata$ppop_foreign_nocit

Sdata$y98_reg_nhisp			 = Sdata$y98_reg_tot - Sdata$y98_reg_hisp
Sdata$y98_preg_nhisp		 = Sdata$y98_reg_nhisp/Sdata$y98_reg_tot

Sdata$y00_turn_nhisp       = Sdata$y00_turn_tot - Sdata$y00_turn_hisp
Sdata$y00_reg_nhisp        = Sdata$y00_reg_tot - Sdata$y00_reg_hisp

Sdata$y04_turn_nhisp       = Sdata$y04_turn_tot - Sdata$y04_turn_hisp
Sdata$y04_reg_nhisp        = Sdata$y04_reg_tot  - Sdata$y04_reg_hisp                   

Sdata$y04_turn_nhisp       = Sdata$y04_turn_tot - Sdata$y04_turn_hisp
Sdata$y04_reg_nhisp        = Sdata$y04_reg_tot  - Sdata$y04_reg_hisp                                                                           
                                                                          
Sdata$y06_turn_nhisp       = Sdata$y06_turn_tot - Sdata$y06_turn_hisp
Sdata$y06_reg_nhisp        = Sdata$y06_reg_tot  - Sdata$y06_reg_hisp         

names(Sdata)[which(names(Sdata)=='tr')]='Tr'
names(WH2data)[which(names(WH2data)=='tr')]='Tr'

  
# CONSTRUCT NHISP 2000 OUTCOME FOR WH2DATA    

#WH2data$y04_reg_nhisp-WH2data$y04_reg_tot-WH2data$y04_reg_hisp
#WH2data$y04_turn_nhisp=WH2data$y04_turn_tot-WH2data$y04_turn_hisp
  
# CONSTRUCT NHISP 2000 OUTCOME FOR SDATA

#Sdata$y04_reg_nhisp=Sdata$y04_reg_tot-Sdata$y04_reg_hisp
#Sdata$y04_turn_nhisp=Sdata$y04_turn_tot-Sdata$y04_turn_hisp


# Conditioning/Balance set: group most important variables first
# HERE
imp.vars <- data.frame(
Sdata$vap,
Sdata$ppop_black18,
Sdata$ppop_hispanic18,
Sdata$phh_income99_39less,
Sdata$phh_income99_40to74,
Sdata$phh_income99_100plus,                   
Sdata$ppop_25_hsless,
Sdata$ppop_foreign

)
# Registration variables: include only 1998
reg.vars <- data.frame(
Sdata$y98_preg_tot,                                              
Sdata$y98_preg_hisp, # no turnout info for 1998, so this is the closest we have to previous outcome
Sdata$y98_preg_dem,
Sdata$y98_preg_rep                       
)
dim(reg.vars)
cat("Final dimension of reg.vars: ", dim(reg.vars), "\n")

# Vote variables

vote.vars <- data.frame(
Sdata$y98_pvote_ussdem,        # statewide and local offices
Sdata$y98_pvote_govdem,
Sdata$y98_pvote_cngdem,                        
Sdata$y98_pvote_assdem,
Sdata$y98_pvote_atgdem
)

dim(vote.vars)
cat("Final dimension of vote.vars: ", dim(vote.vars), "\n")

# Population variables
pop.vars <- data.frame(
Sdata$ppop_fem,
Sdata$ppop_25to44,
Sdata$ppop_45to59,
Sdata$ppop_70older
)
cat("Final dimension of pop.vars: ", dim(pop.vars), "\n")


Xall <- data.frame (imp.vars,reg.vars, vote.vars, pop.vars)  # exclude Sdata$DisTri_1991 since we will keep one unique triplet in every file
X <- data.frame (imp.vars)  # exclude Sdata$DisTri_1991 since we will keep one unique triplet in every file
B <- X
dim(X)

    
# WH2data treatment 
Tr <- WH2data$Tr
m1=c()
m1$index.treated=which(Tr==T)
m1$index.control=which(Tr==F)           
print(table(Tr)) 
    
# Sdata treatment 
Tr <- Sdata$Tr
m2=c()
m2$index.treated=which(Tr==T)
m2$index.control=which(Tr==F)           
print(table(Tr))
                          

######################################################
### Results matrix
######################################################
ACTcolnm <- c("Mean Tr","Mean Co","Diff means p-val", "KS test p-val")

ACTrownm <- c(
"Hispanic Registration 2004 WH2",
"Hispanic Turnout 2004 WH2",
"NonHispanic Registration 2004 WH2", 
"NonHispanic Turnout 2004 WH2",
"Hispanic Registration 2004-2000 Difference WH2",
"Hispanic Turnout 2004-2000 Difference WH2",
"NonHispanic Registration 2004-2000 Difference WH2",
"NonHispanic Turnout 2004-2000 Difference WH2",

"Hispanic Registration 2004 AM",
"Hispanic Turnout 2004 AM",
"NonHispanic Registration 2004 AM", 
"NonHispanic Turnout 2004 AM",
"Hispanic Registration 2004-2000 Difference AM",
"Hispanic Turnout 2004-2000 Difference AM",
"NonHispanic Registration 2004-2000 Difference AM",
"NonHispanic Turnout 2004-2000 Difference AM"
)


RESACT <- matrix(data=NA,nrow=length(ACTrownm), ncol=length(ACTcolnm), dimnames = list(ACTrownm,ACTcolnm))


######################################################
### Sensitivity results matrix
######################################################
SENcolnm <- c("H-L min","H-L max","Gamma","Wilcoxon p-val")
SENcolnm = rep(SENcolnm,times=4)

SENrownm <- c(
"Hispanic Registration 2004 WH2",
"Hispanic Turnout 2004 WH2",
"NonHispanic Registration 2004 WH2", 
"NonHispanic Turnout 2004 WH2",
"Hispanic Registration 2004-2000 Difference WH2",
"Hispanic Turnout 2004-2000 Difference WH2",
"NonHispanic Registration 2004-2000 Difference WH2",
"NonHispanic Turnout 2004-2000 Difference WH2",

"Hispanic Registration 2004 AM",
"Hispanic Turnout 2004 AM",
"NonHispanic Registration 2004 AM", 
"NonHispanic Turnout 2004 AM",
"Hispanic Registration 2004-2000 Difference AM",
"Hispanic Turnout 2004-2000 Difference AM",
"NonHispanic Registration 2004-2000 Difference AM",
"NonHispanic Turnout 2004-2000 Difference AM"
)

SENACT <- matrix(data=NA,nrow=length(SENrownm), ncol=length(SENcolnm), dimnames = list(SENrownm,SENcolnm))


######################
# Sample sizes
#####################
dim(WH2data)
table(WH2data$Tr)


dim(Sdata)
table(Sdata$Tr)


#####################################################################################
## Generate X matrices for later sensitivity tests; using same covariates as in balance tests 
#####################################################################################

WH2data$y00_preg_rep=1-WH2data$y00_preg_dem          
Sdata$y00_preg_rep=1-Sdata$y00_preg_dem     

Sdata$y00_preg_tot = Sdata$y00_reg_tot/Sdata$vap 
WH2data$y00_preg_tot = WH2data$y00_reg_tot/WH2data$vap

WH2data$phh_income99_39less  = rowSums(cbind(WH2data$phh_income99_0to19,WH2data$phh_income99_20to30))
WH2data$phh_income99_40to74  = rowSums(cbind(WH2data$phh_income99_40to59,WH2data$phh_income99_60to74))
WH2data$phh_income99_100plus = rowSums(cbind(WH2data$phh_income99_100to199,WH2data$phh_income99_200))
WH2data$ppop_foreign = rowSums(cbind(WH2data$ppop_foreign_nocit,WH2data$ppop_foreign_naturcit))


Xmat_WH2data <- as.data.frame(cbind(WH2data$vap      
,WH2data$ppop_black18         ,WH2data$ppop_hispanic18      
,WH2data$phh_income99_39less  ,WH2data$phh_income99_40to74  
,WH2data$phh_income99_100plus ,WH2data$ppop_25_hsless      
,WH2data$ppop_foreign         ,WH2data$y98_preg_tot       
,WH2data$y98_preg_hisp        ,WH2data$y98_preg_dem       
,WH2data$y98_preg_rep         ,WH2data$y00_preg_tot      
,WH2data$y00_preg_dem         ,WH2data$y00_preg_rep       
,WH2data$y98_pvote_ussdem     ,WH2data$y98_pvote_govdem   
,WH2data$y98_pvote_cngdem     ,WH2data$y00_vote_pprsdem    
,WH2data$y00_pvote_ussdem     ,WH2data$y00_pvote_cngdem    
,WH2data$ppop_fem             ,WH2data$ppop_25to44        
,WH2data$ppop_45to59))

#Xmat_WH1data <- as.data.frame(cbind(WH1data$vap      
#,WH1data$ppop_black18         ,WH1data$ppop_hispanic18      
#,WH1data$phh_income99_39less  ,WH1data$phh_income99_40to74  
#,WH1data$phh_income99_100plus ,WH1data$ppop_25_hsless      
#,WH1data$ppop_foreign         ,WH1data$y98_preg_tot       
#,WH1data$y98_preg_hisp        ,WH1data$y98_preg_dem       
#,WH1data$y98_preg_rep         ,WH1data$y00_preg_tot      
#,WH1data$y00_preg_dem         ,WH1data$y00_preg_rep       
#,WH1data$y98_pvote_ussdem     ,WH1data$y98_pvote_govdem   
#,WH1data$y98_pvote_cngdem     ,WH1data$y00_vote_pprsdem    
#,WH1data$y00_pvote_ussdem     ,WH1data$y00_pvote_cngdem    
#,WH1data$ppop_fem             ,WH1data$ppop_25to44        
#,WH1data$ppop_45to59))

Xmat_Sdata <- as.data.frame(cbind(Sdata$vap 
,Sdata$ppop_black18         ,Sdata$ppop_hispanic18      
,Sdata$phh_income99_39less  ,Sdata$phh_income99_40to74  
,Sdata$phh_income99_100plus ,Sdata$ppop_25_hsless      
,Sdata$ppop_foreign         ,Sdata$y98_preg_tot       
,Sdata$y98_preg_hisp        ,Sdata$y98_preg_dem       
,Sdata$y98_preg_rep         ,Sdata$y00_preg_tot      
,Sdata$y00_preg_dem         ,Sdata$y00_preg_rep       
,Sdata$y98_pvote_ussdem     ,Sdata$y98_pvote_govdem   
,Sdata$y98_pvote_cngdem     ,Sdata$y00_vote_pprsdem    
,Sdata$y00_pvote_ussdem     ,Sdata$y00_pvote_cngdem    
,Sdata$ppop_fem             ,Sdata$ppop_25to44        
,Sdata$ppop_45to59))
       
data_names=c("vap","ppop_black18","ppop_hispanic18","phh_income99_39less","phh_income99_40to74","phh_income99_100plus",
"ppop_25_hsless","ppop_foreign","y98_preg_tot","y98_preg_hisp","y98_preg_dem","y98_preg_rep","y00_preg_tot","y00_preg_dem",
"y00_preg_rep","y98_pvote_ussdem","y98_pvote_govdem","y98_pvote_cngdem","y00_vote_pprsdem","y00_pvote_ussdem",
"y00_pvote_cngdem","ppop_fem","ppop_25to44","ppop_45to59")

#names(Xmat_WH2data)=names(Xmat_WH1data)=names(Xmat_Sdata)=names(Xmat_mdata)=data_names
names(Xmat_WH2data)=names(Xmat_Sdata)=data_names

dim(Xmat_WH2data)
#dim(Xmat_WH1data)
dim(Xmat_Sdata)   
 

######################################################
### Actual 2004 level results start here
######################################################

################################################################################
### Actual results (i): Hispanic Registration in 2004 (as share of hvap) #######################
################################################################################

#UNMATCHED

Y <- (WH2data$y04_reg_hisp/WH2data$hvap)
Y[which(WH2data$y04_reg_hisp>WH2data$hvap)]<-NA
Y[which(WH2data$y04_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Tr <- WH2data$Tr == 1

Y  <- Y
Tr <- Tr

TrWH2<-Tr[c(m1$index.treated,m1$index.control)]
YWH2<-Y[c(m1$index.treated,m1$index.control)]


#MATCHED

Y <- (Sdata$y04_reg_hisp/Sdata$hvap)
Y[which(Sdata$y04_reg_hisp>Sdata$hvap)]<-NA
Y[which(Sdata$y04_reg_hisp==999999 | Sdata$hvap==0 | Sdata$hvap==999999)]<-NA
Tr <- Sdata$Tr == 1

Y  <- Y
Tr <- Tr

TrS<-Tr[c(m2$index.treated,m2$index.control)]
YS<-Y[c(m2$index.treated,m2$index.control)]





########################
## WH2data: Most naive
#######################
cat("Top of results for WH2data \n")
t = t.test(YWH2[TrWH2], YWH2[!TrWH2])
cat("T-test:\n");  print(t)
ks = ks.boot(YWH2[TrWH2], YWH2[!TrWH2])
cat("KS Boot:\n"); 
print(ks)

RESACT["Hispanic Registration 2004 WH2","Mean Tr"] = t$estimate[1]
RESACT["Hispanic Registration 2004 WH2","Mean Co"] = t$estimate[2]
RESACT["Hispanic Registration 2004 WH2","Diff means p-val"] = t$p.value
RESACT["Hispanic Registration 2004 WH2","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(YWH2[TrWH2], YWH2[!TrWH2],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for WH2data \n")

########################
## WH2data: Sensitivity 
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(YWH2[TrWH2]),size=length(YWH2[TrWH2])*pcnt,replace=F))
ct1=sort(sample(1:length(YWH2[!TrWH2]),size=length(YWH2[!TrWH2])*pcnt,replace=F))

treated=YWH2[TrWH2][tr1]
control=YWH2[!TrWH2][ct1]

Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Registration 2004 WH2",]=outs_tmp


########################
## Matching: Everything in WH1 plus trimming, dropping triplets that do not satisfy inclusion criteria, matching within triplets, stacking data
#######################
cat("Top of results for Matching \n")
t = t.test(YS[TrS], YS[!TrS])
cat("T-test:\n");  print(t)
ks = ks.boot(YS[TrS], YS[!TrS])
cat("KS Boot:\n"); 
print(ks)

RESACT["Hispanic Registration 2004 AM","Mean Tr"] = t$estimate[1]
RESACT["Hispanic Registration 2004 AM","Mean Co"] = t$estimate[2]
RESACT["Hispanic Registration 2004 AM","Diff means p-val"] = t$p.value
RESACT["Hispanic Registration 2004 AM","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(Y[Tr], Y[!Tr],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for Matching \n")

########################
## Sdata: Sensitivity 
#######################



# 100 percent sample for small sample size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(YS[TrS]),size=length(YS[TrS])*pcnt,replace=F))
ct1=sort(sample(1:length(YS[!TrS]),size=length(YS[!TrS])*pcnt,replace=F))

treated=YS[TrS][tr1]
control=YS[!TrS][ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Registration 2004 AM",]=outs_tmp


################################################################################
### Actual results (ii): Hispanic Turnout in 2004 (as share of hvap) #######################
################################################################################

#UNMATCHED

Y <- (WH2data$y04_turn_hisp/WH2data$y00_reg_hisp)
Y[which(WH2data$y04_turn_hisp>WH2data$y00_reg_hisp)]<-NA
Y[which(WH2data$y04_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA
Tr <- WH2data$Tr == 1

Y  <- Y
Tr <- Tr

TrWH2<-Tr[c(m1$index.treated,m1$index.control)]
YWH2<-Y[c(m1$index.treated,m1$index.control)]


#MATCHED

Y <- (Sdata$y04_turn_hisp/Sdata$hvap)
Y[which(Sdata$y04_turn_hisp>Sdata$hvap)]<-NA
Y[which(Sdata$y04_turn_hisp==999999 | Sdata$hvap==0 | Sdata$hvap==999999)]<-NA
Tr <- Sdata$Tr == 1

Y  <- Y
Tr <- Tr

TrS<-Tr[c(m2$index.treated,m2$index.control)]
YS<-Y[c(m2$index.treated,m2$index.control)]


########################
## WH2data: Most naive
#######################
cat("Top of results for WH2data \n")
t = t.test(YWH2[TrWH2], YWH2[!TrWH2])
cat("T-test:\n");  print(t)
ks = ks.boot(YWH2[TrWH2], YWH2[!TrWH2])
cat("KS Boot:\n"); 
print(ks)

RESACT["Hispanic Turnout 2004 WH2","Mean Tr"] = t$estimate[1]
RESACT["Hispanic Turnout 2004 WH2","Mean Co"] = t$estimate[2]
RESACT["Hispanic Turnout 2004 WH2","Diff means p-val"] = t$p.value
RESACT["Hispanic Turnout 2004 WH2","KS test p-val"] = ks$ks.boot.pvalue


#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(YWH2[TrWH2], YWH2[!TrWH2],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for WH2data \n")

########################
## WH2data: Sensitivity 
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(YWH2[TrWH2]),size=length(YWH2[TrWH2])*pcnt,replace=F))
ct1=sort(sample(1:length(YWH2[!TrWH2]),size=length(YWH2[!TrWH2])*pcnt,replace=F))

treated=YWH2[TrWH2][tr1]
control=YWH2[!TrWH2][ct1]


Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Turnout 2004 WH2",]=outs_tmp



########################
## Matching: Everything in WH1 plus trimming, dropping triplets that do not satisfy inclusion criteria, matching within triplets, stacking data
#######################
cat("Top of results for Matching \n")
t = t.test(YS[TrS], YS[!TrS])
cat("T-test:\n");  print(t)
ks = ks.boot(YS[TrS], YS[!TrS])
cat("KS Boot:\n"); 
print(ks)

RESACT["Hispanic Turnout 2004 AM","Mean Tr"] = t$estimate[1]
RESACT["Hispanic Turnout 2004 AM","Mean Co"] = t$estimate[2]
RESACT["Hispanic Turnout 2004 AM","Diff means p-val"] = t$p.value
RESACT["Hispanic Turnout 2004 AM","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(Y[Tr], Y[!Tr],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for Matching \n")

########################
## Sdata: Sensitivity 
#######################



# 100 percent sample for small sample size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(YS[TrS]),size=length(YS[TrS])*pcnt,replace=F))
ct1=sort(sample(1:length(YS[!TrS]),size=length(YS[!TrS])*pcnt,replace=F))

treated=YS[TrS][tr1]
control=YS[!TrS][ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Turnout 2004 AM",]=outs_tmp


################################################################################
### Actual results (iii): NonHispanic Turnout in 2004 (as share of nhvap) #######################
################################################################################

#UNMATCHED

Y <- (WH2data$y04_turn_nhisp/WH2data$y00_reg_nhisp)
Y[which(WH2data$y04_turn_nhisp>WH2data$y00_reg_nhisp)]<-NA
Y[which(WH2data$y04_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA
Tr <- WH2data$Tr == 1

Y  <- Y
Tr <- Tr

TrWH2<-Tr[c(m1$index.treated,m1$index.control)]
YWH2<-Y[c(m1$index.treated,m1$index.control)]


#MATCHED

Y <- (Sdata$y04_turn_nhisp/Sdata$nhvap)
Y[which(Sdata$y04_turn_nhisp>Sdata$nhvap)]<-NA
Y[which(Sdata$y04_turn_nhisp==999999 | Sdata$nhvap==0 | Sdata$nhvap==999999)]<-NA
Tr <- Sdata$Tr == 1

Y  <- Y
Tr <- Tr

TrS<-Tr[c(m2$index.treated,m2$index.control)]
YS<-Y[c(m2$index.treated,m2$index.control)]

 
########################
## WH2data: Most naive
#######################
cat("Top of results for WH2data \n")
t = t.test(YWH2[TrWH2], YWH2[!TrWH2])
cat("T-test:\n");  print(t)
ks = ks.boot(YWH2[TrWH2], YWH2[!TrWH2])
cat("KS Boot:\n"); 
print(ks)

RESACT["NonHispanic Turnout 2004 WH2","Mean Tr"] = t$estimate[1]
RESACT["NonHispanic Turnout 2004 WH2","Mean Co"] = t$estimate[2]
RESACT["NonHispanic Turnout 2004 WH2","Diff means p-val"] = t$p.value
RESACT["NonHispanic Turnout 2004 WH2","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(YWH2[TrWH2], YWH2[!TrWH2],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for WH2data \n")

########################
## WH2data: Sensitivity 
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(YWH2[TrWH2]),size=length(YWH2[TrWH2])*pcnt,replace=F))
ct1=sort(sample(1:length(YWH2[!TrWH2]),size=length(YWH2[!TrWH2])*pcnt,replace=F))

treated=YWH2[TrWH2][tr1]
control=YWH2[!TrWH2][ct1]

Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Turnout 2004 WH2",]=outs_tmp


########################
## Matching: Everything in WH1 plus trimming, dropping triplets that do not satisfy inclusion criteria, matching within triplets, stacking data
#######################
cat("Top of results for Matching \n")
t = t.test(YS[TrS], YS[!TrS])
cat("T-test:\n");  print(t)
ks = ks.boot(YS[TrS], YS[!TrS])
cat("KS Boot:\n"); 
print(ks)

RESACT["NonHispanic Turnout 2004 AM","Mean Tr"] = t$estimate[1]
RESACT["NonHispanic Turnout 2004 AM","Mean Co"] = t$estimate[2]
RESACT["NonHispanic Turnout 2004 AM","Diff means p-val"] = t$p.value
RESACT["NonHispanic Turnout 2004 AM","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(Y[Tr], Y[!Tr],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for Matching \n")

########################
## Sdata: Sensitivity 
#######################



# 100 percent sample for small sample size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(YS[TrS]),size=length(YS[TrS])*pcnt,replace=F))
ct1=sort(sample(1:length(YS[!TrS]),size=length(YS[!TrS])*pcnt,replace=F))

treated=YS[TrS][tr1]
control=YS[!TrS][ct1]


Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Turnout 2004 AM",]=outs_tmp
    

################################################################################
### Actual results (iv): NonHispanic Registration in 2004 (as share of nhvap) #######################
################################################################################

#UNMATCHED

Y <- (WH2data$y04_reg_nhisp/WH2data$nhvap)
Y[which(WH2data$y04_reg_nhisp>WH2data$nhvap)]<-NA
Y[which(WH2data$y04_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Tr <- WH2data$Tr == 1

Y  <- Y
Tr <- Tr

TrWH2<-Tr[c(m1$index.treated,m1$index.control)]
YWH2<-Y[c(m1$index.treated,m1$index.control)]


#MATCHED

Y <- (Sdata$y04_reg_nhisp/Sdata$nhvap)
Y[which(Sdata$y04_reg_nhisp>Sdata$nhvap)]<-NA
Y[which(Sdata$y04_reg_nhisp==999999 | Sdata$nhvap==0 | Sdata$nhvap==999999)]<-NA
Tr <- Sdata$Tr == 1

Y  <- Y
Tr <- Tr

TrS<-Tr[c(m2$index.treated,m2$index.control)]
YS<-Y[c(m2$index.treated,m2$index.control)]
   

########################
## WH2data: Most naive
#######################
cat("Top of results for WH2data \n")
t = t.test(YWH2[TrWH2], YWH2[!TrWH2])
cat("T-test:\n");  print(t)
ks = ks.boot(YWH2[TrWH2], YWH2[!TrWH2])
cat("KS Boot:\n"); 
print(ks)

RESACT["NonHispanic Registration 2004 WH2","Mean Tr"] = t$estimate[1]
RESACT["NonHispanic Registration 2004 WH2","Mean Co"] = t$estimate[2]
RESACT["NonHispanic Registration 2004 WH2","Diff means p-val"] = t$p.value
RESACT["NonHispanic Registration 2004 WH2","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(YWH2[TrWH2], YWH2[!TrWH2],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for WH2data \n")

########################
## WH2data: Sensitivity 
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(YWH2[TrWH2]),size=length(YWH2[TrWH2])*pcnt,replace=F))
ct1=sort(sample(1:length(YWH2[!TrWH2]),size=length(YWH2[!TrWH2])*pcnt,replace=F))

treated=YWH2[TrWH2][tr1]
control=YWH2[!TrWH2][ct1]


Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  


# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Registration 2004 WH2",]=outs_tmp



########################
## Matching: Everything in WH1 plus trimming, dropping triplets that do not satisfy inclusion criteria, matching within triplets, stacking data
#######################
cat("Top of results for Matching \n")
t = t.test(YS[TrS], YS[!TrS])
cat("T-test:\n");  print(t)
ks = ks.boot(YS[TrS], YS[!TrS])
cat("KS Boot:\n"); 
print(ks)

RESACT["NonHispanic Registration 2004 AM","Mean Tr"] = t$estimate[1]
RESACT["NonHispanic Registration 2004 AM","Mean Co"] = t$estimate[2]
RESACT["NonHispanic Registration 2004 AM","Diff means p-val"] = t$p.value
RESACT["NonHispanic Registration 2004 AM","KS test p-val"] = ks$ks.boot.pvalue

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(Y[Tr], Y[!Tr],exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of results for Matching \n")

########################
## Sdata: Sensitivity 
#######################



# 100 percent sample for small sample size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(YS[TrS]),size=length(YS[TrS])*pcnt,replace=F))
ct1=sort(sample(1:length(YS[!TrS]),size=length(YS[!TrS])*pcnt,replace=F))

treated=YS[TrS][tr1]
control=YS[!TrS][ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Registration 2004 AM",]=outs_tmp



######################################################
### Diff-in-Diff Results 
######################################################

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

########################
## WH2data: Most naive
#######################

#UNMATCHED

Y1 <- (WH2data$y04_reg_hisp/WH2data$hvap)
Y0 <- (WH2data$y00_reg_hisp/WH2data$hvap)

Y1[which(WH2data$y04_reg_hisp/WH2data$hvap > 1)]<-NA
Y0[which(WH2data$y00_reg_hisp/WH2data$hvap > 1)]<-NA

Y1[which(WH2data$y04_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA
Y0[which(WH2data$y00_reg_hisp==999999 | WH2data$hvap==0 | WH2data$hvap==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

##if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["Hispanic Registration 2004-2000 Difference WH2","Mean Tr"] = ddWH2.Tr 
RESACT["Hispanic Registration 2004-2000 Difference WH2","Mean Co"] = ddWH2.Co
#RESACT["Hispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = t$p.value
RESACT["Hispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["Hispanic Registration 2004-2000 Difference WH2","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of WH2 results \n")

########################
## WH2data: Sensitivity Dif-in-Dif
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]


Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Registration 2004-2000 Difference WH2",]=outs_tmp


######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

########################
## WH2data: Most naive
#######################

#UNMATCHED

Y1 <- (WH2data$y04_turn_hisp/WH2data$y00_reg_hisp)
Y0 <- (WH2data$y00_turn_hisp/WH2data$y00_reg_hisp)

Y1[which(WH2data$y04_turn_hisp/WH2data$y00_reg_hisp > 1)]<-NA
Y0[which(WH2data$y00_turn_hisp/WH2data$y00_reg_hisp > 1)]<-NA

Y1[which(WH2data$y04_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA
Y0[which(WH2data$y00_turn_hisp==999999 | WH2data$y00_reg_hisp==0 | WH2data$y00_reg_hisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

##if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["Hispanic Turnout 2004-2000 Difference WH2","Mean Tr"] = ddWH2.Tr 
RESACT["Hispanic Turnout 2004-2000 Difference WH2","Mean Co"] = ddWH2.Co
#RESACT["Hispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = t$p.value
RESACT["Hispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["Hispanic Turnout 2004-2000 Difference WH2","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of WH2 results \n")

########################
## WH2data: Sensitivity Dif-in-Dif
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]


Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Turnout 2004-2000 Difference WH2",]=outs_tmp

######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

########################
## WH2data: Most naive
#######################

#UNMATCHED

Y1 <- (WH2data$y04_reg_nhisp/WH2data$nhvap)
Y0 <- (WH2data$y00_reg_nhisp/WH2data$nhvap)

Y1[which(WH2data$y04_reg_nhisp/WH2data$nhvap > 1)]<-NA
Y0[which(WH2data$y00_reg_nhisp/WH2data$nhvap > 1)]<-NA

Y1[which(WH2data$y04_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA
Y0[which(WH2data$y00_reg_nhisp==999999 | WH2data$nhvap==0 | WH2data$nhvap==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]

ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

##if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["NonHispanic Registration 2004-2000 Difference WH2","Mean Tr"] = ddWH2.Tr 
RESACT["NonHispanic Registration 2004-2000 Difference WH2","Mean Co"] = ddWH2.Co
#RESACT["NonHispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = t$p.value
RESACT["NonHispanic Registration 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["NonHispanic Registration 2004-2000 Difference WH2","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of WH2 results \n")

########################
## WH2data: Sensitivity Dif-in-Dif
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]


Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Registration 2004-2000 Difference WH2",]=outs_tmp

######################################################
### (iv) Diff-in-Diff results on NonHispanic Turnout
######################################################

########################
## WH2data: Most naive
#######################

#UNMATCHED

Y1 <- (WH2data$y04_turn_nhisp/WH2data$y00_reg_nhisp)
Y0 <- (WH2data$y00_turn_nhisp/WH2data$y00_reg_nhisp)

Y1[which(WH2data$y04_turn_nhisp/WH2data$y00_reg_nhisp > 1)]<-NA
Y0[which(WH2data$y00_turn_nhisp/WH2data$y00_reg_nhisp > 1)]<-NA

Y1[which(WH2data$y04_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA
Y0[which(WH2data$y00_turn_nhisp==999999 | WH2data$y00_reg_nhisp==0 | WH2data$y00_reg_nhisp==999999)]<-NA

Y1 <- Y1[c(m1$index.treated,m1$index.control)]
Y0 <- Y0[c(m1$index.treated,m1$index.control)]

Tr <- WH2data$Tr == 1
TrWH2<-Tr[c(m1$index.treated,m1$index.control)]


YWH2.1.Tr=Y1[TrWH2]
YWH2.1.Co=Y1[!TrWH2]
YWH2.0.Tr=Y0[TrWH2]
YWH2.0.Co=Y0[!TrWH2]


ddWH2.Tr <- mean(na.rm=T,YWH2.1.Tr) - mean(na.rm=T,YWH2.0.Tr)
ddWH2.Co <- mean(na.rm=T,YWH2.1.Co) - mean(na.rm=T,YWH2.0.Co)
ddWH2 <- ddWH2.Tr - ddWH2.Co 
print(ddWH2)

YY <- c(YWH2.1.Tr, YWH2.1.Co,YWH2.0.Tr, YWH2.0.Co)
time <- c(rep(1,length(c(YWH2.1.Tr, YWH2.1.Co))),rep(0,length(c(YWH2.0.Tr, YWH2.0.Co))))
TT <- c(rep(1,length(YWH2.1.Tr)),rep(0,length(YWH2.1.Co)),rep(1,length(YWH2.0.Tr)),rep(0,length(YWH2.0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = YWH2.1.Tr - YWH2.0.Tr
DCo = YWH2.1.Co - YWH2.0.Co
D1 = YWH2.1.Tr - YWH2.1.Co  
D0 = YWH2.0.Tr - YWH2.0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

##if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["NonHispanic Turnout 2004-2000 Difference WH2","Mean Tr"] = ddWH2.Tr 
RESACT["NonHispanic Turnout 2004-2000 Difference WH2","Mean Co"] = ddWH2.Co
#RESACT["NonHispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = t$p.value
RESACT["NonHispanic Turnout 2004-2000 Difference WH2","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["NonHispanic Turnout 2004-2000 Difference WH2","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of WH2 results \n")

########################
## WH2data: Sensitivity Dif-in-Dif
#######################



# 10 percent sample for large sample size  # full run here
if(ties_used==T){
	pcnt=.1
} else{
	pcnt=1
}

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]


Xmat=Xmat_WH2data
Xmat_tr=Xmat[m1$index.treated,]
Xmat_ct=Xmat[m1$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Turnout 2004-2000 Difference WH2",]=outs_tmp

######################################################
### (i) Diff-in-Diff results on Hispanic Registration
######################################################

#MATCHED

Y1 <- (Sdata$y04_reg_hisp/Sdata$hvap)
Y0 <- (Sdata$y00_reg_hisp/Sdata$hvap)

Y1[which(Sdata$y04_reg_hisp/Sdata$hvap > 1)]<-NA
Y0[which(Sdata$y00_reg_hisp/Sdata$hvap > 1)]<-NA

Y1[which(Sdata$y04_reg_hisp==999999 | Sdata$hvap==0 | Sdata$hvap==999999)]<-NA
Y0[which(Sdata$y00_reg_hisp==999999 | Sdata$hvap==0 | Sdata$hvap==999999)]<-NA

Y1 <- Y1[c(m2$index.treated,m2$index.control)]
Y0 <- Y0[c(m2$index.treated,m2$index.control)]

Tr <- Sdata$Tr == 1
TrS<-Tr[c(m2$index.treated,m2$index.control)]

Y1.Tr=Y1[TrS]
Y1.Co=Y1[!TrS]
Y0.Tr=Y0[TrS]
Y0.Co=Y0[!TrS]


uddTr <- mean(na.rm=T,Y1.Tr) - mean(na.rm=T,Y0.Tr)
uddCo <- mean(na.rm=T,Y1.Co) - mean(na.rm=T,Y0.Co)
udd <- uddTr - uddCo
print(udd)

YY <- c(Y1.Tr, Y1.Co,Y0.Tr, Y0.Co)
time <- c(rep(1,length(c(Y1.Tr, Y1.Co))),rep(0,length(c(Y0.Tr, Y0.Co))))
TT <- c(rep(1,length(Y1.Tr)),rep(0,length(Y1.Co)),rep(1,length(Y0.Tr)),rep(0,length(Y0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1.Tr - Y0.Tr
DCo = Y1.Co - Y0.Co
D1 = Y1.Tr - Y1.Co
D0 = Y0.Tr - Y0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

#if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["Hispanic Registration 2004-2000 Difference AM","Mean Tr"] = uddTr 
RESACT["Hispanic Registration 2004-2000 Difference AM","Mean Co"] = uddCo
#RESACT["Hispanic Registration 2004-2000 Difference AM","Diff means p-val"] = t$p.value
RESACT["Hispanic Registration 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["Hispanic Registration 2004-2000 Difference AM","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of intermediate matching results \n")




########################
## Sdata: Sensitivity Dif-in-Dif
#######################



# 100 percent sample for large small size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Registration 2004-2000 Difference AM",]=outs_tmp

######################################################
### (ii) Diff-in-Diff results on Hispanic Turnout
######################################################

#MATCHED

Y1 <- (Sdata$y04_turn_hisp/Sdata$hvap)
Y0 <- (Sdata$y00_turn_hisp/Sdata$hvap)

Y1[which(Sdata$y04_turn_hisp/Sdata$hvap > 1)]<-NA
Y0[which(Sdata$y00_turn_hisp/Sdata$hvap > 1)]<-NA

Y1[which(Sdata$y04_turn_hisp==999999 | Sdata$hvap==0 | Sdata$hvap==999999)]<-NA
Y0[which(Sdata$y00_turn_hisp==999999 | Sdata$hvap==0 | Sdata$hvap==999999)]<-NA

Y1 <- Y1[c(m2$index.treated,m2$index.control)]
Y0 <- Y0[c(m2$index.treated,m2$index.control)]

Tr <- Sdata$Tr == 1
TrS<-Tr[c(m2$index.treated,m2$index.control)]

Y1.Tr=Y1[TrS]
Y1.Co=Y1[!TrS]
Y0.Tr=Y0[TrS]
Y0.Co=Y0[!TrS]


uddTr <- mean(na.rm=T,Y1.Tr) - mean(na.rm=T,Y0.Tr)
uddCo <- mean(na.rm=T,Y1.Co) - mean(na.rm=T,Y0.Co)
udd <- uddTr - uddCo
print(udd)

YY <- c(Y1.Tr, Y1.Co,Y0.Tr, Y0.Co)
time <- c(rep(1,length(c(Y1.Tr, Y1.Co))),rep(0,length(c(Y0.Tr, Y0.Co))))
TT <- c(rep(1,length(Y1.Tr)),rep(0,length(Y1.Co)),rep(1,length(Y0.Tr)),rep(0,length(Y0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1.Tr - Y0.Tr
DCo = Y1.Co - Y0.Co
D1 = Y1.Tr - Y1.Co
D0 = Y0.Tr - Y0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

#if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["Hispanic Turnout 2004-2000 Difference AM","Mean Tr"] = uddTr 
RESACT["Hispanic Turnout 2004-2000 Difference AM","Mean Co"] = uddCo
#RESACT["Hispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = t$p.value
RESACT["Hispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["Hispanic Turnout 2004-2000 Difference AM","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of intermediate matching results \n")



########################
## Sdata: Sensitivity Dif-in-Dif
#######################



# 100 percent sample for large small size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["Hispanic Turnout 2004-2000 Difference AM",]=outs_tmp


######################################################
### (iii) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED

Y1 <- (Sdata$y04_turn_nhisp/Sdata$nhvap)
Y0 <- (Sdata$y00_turn_nhisp/Sdata$nhvap)

Y1[which(Sdata$y04_turn_nhisp/Sdata$nhvap > 1)]<-NA
Y0[which(Sdata$y00_turn_nhisp/Sdata$nhvap > 1)]<-NA

Y1[which(Sdata$y04_turn_nhisp==999999 | Sdata$nhvap==0 | Sdata$nhvap==999999)]<-NA
Y0[which(Sdata$y00_turn_nhisp==999999 | Sdata$nhvap==0 | Sdata$nhvap==999999)]<-NA

Y1 <- Y1[c(m2$index.treated,m2$index.control)]
Y0 <- Y0[c(m2$index.treated,m2$index.control)]

Tr <- Sdata$Tr == 1
TrS<-Tr[c(m2$index.treated,m2$index.control)]

Y1.Tr=Y1[TrS]
Y1.Co=Y1[!TrS]
Y0.Tr=Y0[TrS]
Y0.Co=Y0[!TrS]


uddTr <- mean(na.rm=T,Y1.Tr) - mean(na.rm=T,Y0.Tr)
uddCo <- mean(na.rm=T,Y1.Co) - mean(na.rm=T,Y0.Co)
udd <- uddTr - uddCo
print(udd)

YY <- c(Y1.Tr, Y1.Co,Y0.Tr, Y0.Co)
time <- c(rep(1,length(c(Y1.Tr, Y1.Co))),rep(0,length(c(Y0.Tr, Y0.Co))))
TT <- c(rep(1,length(Y1.Tr)),rep(0,length(Y1.Co)),rep(1,length(Y0.Tr)),rep(0,length(Y0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1.Tr - Y0.Tr
DCo = Y1.Co - Y0.Co
D1 = Y1.Tr - Y1.Co
D0 = Y0.Tr - Y0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

#if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["NonHispanic Registration 2004-2000 Difference AM","Mean Tr"] = uddTr 
RESACT["NonHispanic Registration 2004-2000 Difference AM","Mean Co"] = uddCo
#RESACT["NonHispanic Registration 2004-2000 Difference AM","Diff means p-val"] = t$p.value
RESACT["NonHispanic Registration 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["NonHispanic Registration 2004-2000 Difference AM","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of intermediate matching results \n")



########################
## Sdata: Sensitivity Dif-in-Dif
#######################



# 100 percent sample for large small size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Registration 2004-2000 Difference AM",]=outs_tmp


######################################################
### (iv) Diff-in-Diff results on NonHispanic Registration
######################################################

#MATCHED

Y1 <- (Sdata$y04_reg_nhisp/Sdata$nhvap)
Y0 <- (Sdata$y00_reg_nhisp/Sdata$nhvap)

Y1[which(Sdata$y04_reg_nhisp/Sdata$nhvap > 1)]<-NA
Y0[which(Sdata$y00_reg_nhisp/Sdata$nhvap > 1)]<-NA

Y1[which(Sdata$y04_reg_nhisp==999999 | Sdata$nhvap==0 | Sdata$nhvap==999999)]<-NA
Y0[which(Sdata$y00_reg_nhisp==999999 | Sdata$nhvap==0 | Sdata$nhvap==999999)]<-NA

Y1 <- Y1[c(m2$index.treated,m2$index.control)]
Y0 <- Y0[c(m2$index.treated,m2$index.control)]

Tr <- Sdata$Tr == 1
TrS<-Tr[c(m2$index.treated,m2$index.control)]

Y1.Tr=Y1[TrS]
Y1.Co=Y1[!TrS]
Y0.Tr=Y0[TrS]
Y0.Co=Y0[!TrS]

uddTr <- mean(na.rm=T,Y1.Tr) - mean(na.rm=T,Y0.Tr)
uddCo <- mean(na.rm=T,Y1.Co) - mean(na.rm=T,Y0.Co)
udd <- uddTr - uddCo
print(udd)

YY <- c(Y1.Tr, Y1.Co,Y0.Tr, Y0.Co)
time <- c(rep(1,length(c(Y1.Tr, Y1.Co))),rep(0,length(c(Y0.Tr, Y0.Co))))
TT <- c(rep(1,length(Y1.Tr)),rep(0,length(Y1.Co)),rep(1,length(Y0.Tr)),rep(0,length(Y0.Co)))
lmout = summary(lm(YY~time+TT+I(time*TT)))
print(lmout)

DTr = Y1.Tr - Y0.Tr
DCo = Y1.Co - Y0.Co
D1 = Y1.Tr - Y1.Co
D0 = Y0.Tr - Y0.Co

t = t.test(DTr, DCo)
cat("T-test:\n");  print(t)
ks = ks.boot(DTr, DCo)
cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(DTr, DCo,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")

#if(abs(t$estimate[1]-t$estimate[2]-lmout$coefficients["I(time * TT)","Estimate"])>1e-8) stop("Error!")
RESACT["NonHispanic Turnout 2004-2000 Difference AM","Mean Tr"] = uddTr 
RESACT["NonHispanic Turnout 2004-2000 Difference AM","Mean Co"] = uddCo
#RESACT["NonHispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = t$p.value
RESACT["NonHispanic Turnout 2004-2000 Difference AM","Diff means p-val"] = lmout$coefficients["I(time * TT)","Pr(>|t|)"]
RESACT["NonHispanic Turnout 2004-2000 Difference AM","KS test p-val"] = ks$ks.boot.pvalue

#t = t.test(D1, D0)
#cat("T-test:\n");  print(t)
#ks = ks.boot(D1, D0)
#cat("KS Boot:\n"); print(ks)

#cat("Hodges-Lehmann\n")
#cat("Using option exact=FALSE\n")
#a <- wilcox.exact(D1, D0,exact=FALSE, paired=FALSE, conf.int=TRUE, conf.level=0.95)
#cat("H-L estimate:",a$estimate,"\n")
#cat("H-L CI:",a$conf.int,"\n")
#cat("H-L p.value:",a$p.value,"\n")
#cat("Bottom of intermediate matching results \n")



########################
## Sdata: Sensitivity Dif-in-Dif
#######################



# 100 percent sample for large small size
pcnt=1

set.seed(57459)
tr1=sort(sample(1:length(DTr),size=length(DTr)*pcnt,replace=F))
ct1=sort(sample(1:length(DCo),size=length(DCo)*pcnt,replace=F))

treated=(DTr)[tr1]
control=(DCo)[ct1]

Xmat=Xmat_Sdata
Xmat_tr=Xmat[m2$index.treated,]
Xmat_ct=Xmat[m2$index.control,]

if(length(treated)!=length(control)){
	matched=F
} else if(length(treated)==length(control)){
	matched=T
}

if(imputes==T){
	inds.na=which(is.na(treated) | is.na(control))
	treated=treated[-c(inds.na)]
	control=control[-c(inds.na)] 
	xtr=Xmat_tr[-c(inds.na),]
	xct=Xmat_ct[-c(inds.na),]
} else{
	# mean impute for the few missings
	treated[is.na(treated)]=mean(na.rm=T,treated)
	control[is.na(control)]=mean(na.rm=T,control)
	xtr=Xmat_tr
	xct=Xmat_ct	
}  

# full & hispanic registration in 1998 (cols = 10)

outs_full=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr,Xmat_ct=xct,inc=.0001,digits=5,matched=matched)
outs_hisp=amplifyObserved(treated=treated,control=control,Xmat_tr=xtr[,10],Xmat_ct=xct[,10],inc=.0001,digits=5,matched=matched)

p_full=outs_full$pvalues$one_tailed
#p_full[which(p_full<outs_full$pvalues$one_tailed)]=outs_full$pvalues$one_tailed[which(p_full<outs_full$pvalues$one_tailed)]
p_hisp=outs_hisp$pvalues$one_tailed
#p_hisp[which(p_hisp<outs_hisp$pvalues$one_tailed)]=outs_hisp$pvalues$one_tailed[which(p_hisp<outs_hisp$pvalues$one_tailed)]

# full, hisp
# med, mean, med, mean

outs_tmp=c(c(outs_full$HL[c(1,3)],outs_full$Gamma[c(1)],p_full[1]),
c(outs_full$HL[c(2,4)],outs_full$Gamma[c(2)],p_full[2]),
c(outs_hisp$HL[c(1,3)],outs_hisp$Gamma[c(1)],p_hisp[1]),
c(outs_hisp$HL[c(2,4)],outs_hisp$Gamma[c(2)],p_hisp[2]))


SENACT["NonHispanic Turnout 2004-2000 Difference AM",]=outs_tmp


######################################################
### Actual results end here
######################################################


######################################################
### Print and convert to latex
######################################################
print(RESACT)

#print(xtable(round(RESACT,digits=4)), type="latex", file="tables-WH-RESACT-outcomes3.tex", append=FALSE, caption.placement="top",
#latex.environments=c("center"),tabular.environment = "tabular", size=NULL, NA.string = "", include.rownames=TRUE,include.colnames=TRUE)



print(SENACT)

#print(xtable(round(SENACT,digits=4)), type="latex", file="tables-WH-SENACT-outcomes3-ONE-TAILED-HVAP-PPOP18.tex", append=FALSE, caption.placement="top",
#latex.environments=c("center"),tabular.environment = "tabular", size=NULL, NA.string = "", include.rownames=TRUE,include.colnames=TRUE)
